package cn.lcsjsxy.yxz.day.IO;

import java.io.*;

//缓冲流
public class bufferstream {
    public static void main(String[] args) {

        //文件复制~缓冲

        // TODO 源文件
        File srcFile = new File("D:\\yang\\idea\\day10\\data\\word.txt");
        // TODO 数据目的地文件对象(自动生成)
        File destFile = new File("D:\\yang\\idea\\day10\\data\\word.txt.copy");

        //缓冲输入流(管道对象)
        FileInputStream in = null;
        //缓冲输出流(管道对象)
        FileOutputStream out = null;

        //文件输入流(管道对象)
        BufferedInputStream buffIn = null;
        //文件输出流(管道对象)
        BufferedOutputStream buffout = null;

        //缓冲区
        byte[] cache = new byte[1024];

        try {
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(destFile);

            buffIn = new BufferedInputStream(in);
            buffout = new  BufferedOutputStream(out);

            //打开阀门,流转数据(输入)
            int data = in.read();
            //打开阀门,流转数据(输出)
            out.write(data);

//            data = in.read();
//            out.write(data);
//            data = in.read();
//            out.write(data);
//            data = in.read();
//            out.write(data);
//            data = in.read();
//            out.write(data);

            //如果文件数据已经全部读取完毕后，那么再去读取数据，读取的结果就是-1，表示无效(结尾)
//            data = in.read();
//            if (data != -1){
//                out.write(data);
//            }

            //结合
            while ((data = buffIn.read(cache)) != -1){
                buffout.write(cache, 0, data);
            }

        }catch (IOException e){
            throw new RuntimeException(e);
        }finally {
            if (buffIn != null){
                try {
                    buffIn.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (buffout != null){
                try {
                    buffout.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }








    }

}
